package querysrc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.subway.babynote.syncdata.*;


public class query_today_avg extends query {
	public query_today_avg(sqlConnectionHandler sqlconhandler)
	{
		super(sqlconhandler);
	}	
	
	//public List<ClientDataRecord.last_avgRecord> get_today_avg(int user_id)
	public List<ClientDataRecordBase> get_today_avg(int user_id)
	{
		Connection con = null;	
		ResultSet rs = null;
		querydb qdb = null;
		ArrayList<ClientDataRecordBase> last_avgs = new ArrayList<ClientDataRecordBase>();
		try
		{
			boolean bret = true;
			con = this.init();	
			
			qdb = new querydb(con,sqlscripts.sql_last_avg_get_today);		
			bret = qdb.prePS();
			if(bret)
			{					
				qdb.ps.setInt(1, user_id);
				rs = qdb.execPSQuery();
				if(rs.next())
				{
					rs.beforeFirst();
					while(rs.next())
					{
						int fpid = rs.getInt(sqlscripts.sql_last_avg_get_today_c1_food_prop_id);
						java.sql.Date rtime = StringUtil.str2dateonly( rs.getString(sqlscripts.sql_last_avg_get_today_c2_r_date) );
						String food_prop = rs.getString(sqlscripts.sql_last_avg_get_today_c3_food_prop);
						float yours = rs.getFloat(sqlscripts.sql_last_avg_get_today_c4_yours);
						float avgs = rs.getFloat(sqlscripts.sql_last_avg_get_today_c5_avgs);
						float diffs = rs.getFloat(sqlscripts.sql_last_avg_get_today_c6_diffs);							
						
						last_avgs.add(new ClientDataRecord.last_avgRecord(fpid, rtime,food_prop,yours, avgs, diffs));
					}
				}
			}
				
			
			
			
					
			return last_avgs;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		    return last_avgs;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return last_avgs;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);
			this.release_connection(con);
			this.finalize();
		}
	}
	
	protected int getAge(Connection con, int user_id, String qstr)
	{
		querydb qdb = null;
		ResultSet rs = null;
		try
		{
			int iret = -1;
			
			qdb = new querydb(con,qstr);
			boolean bret = qdb.prePS();
			if(bret)
			{
				qdb.ps.setInt(1,user_id);
				rs = qdb.execPSQuery();
				if(rs.next())
				{
					int age = rs.getInt(sqlscripts.sql_get_birth_month_c1);
					iret = age;
				}				
			}
			
			return iret;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);
		}
	}
	
	
}
